#include "timerthread.h"


#include <QDebug>

using namespace Moolink;

TimerThread::TimerThread(QObject *parent) :
    QThread(parent)
{

}


void TimerThread::setCallbackFunc(void(*func) (qint64 UtcSec))
{
    m_callbackFunc = func;
}


//! [0]
TimerThread::~TimerThread()
{
    m_mutex.lock();
    m_quit = true;
    m_cond.wakeOne();
    m_mutex.unlock();
    wait();
}
//! [0]

//! [1] //! [2]
void TimerThread::TimerRun(int waitTimeout)//, const QString &request)
{
//! [1]
    const QMutexLocker locker(&m_mutex);



    m_waitTimeout = waitTimeout;


    if (!isRunning())
        start();
    else
        m_cond.wakeOne();
}


void TimerThread::run()
{
    QDateTime origin_time = QDateTime::fromString("1970-01-01 08:00:00","yyyy-MM-dd hh:mm:ss");
    QDateTime current_time;
    qint64 nSeconds;
    qDebug()<<"timerthread";
    while (!m_quit) {

        m_mutex.lock();
        m_cond.wait(&m_mutex,1000);
        m_mutex.unlock();
        current_time = QDateTime::currentDateTime();
        nSeconds = origin_time.secsTo(current_time);//获取距离1970-01-01 08:00:00的总秒数
        m_callbackFunc(nSeconds);
    }

}

void TimerThread::slot_time()
{
//    QTime mytime = QTime::currentTime() ;
//    emit sendtime(mytime.toString()) ;
    qDebug()<<"timerthread";
    //emit this->response2(1);
    m_callbackFunc(1);
}
